package com.example.bob.usinlistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by Bob on 2016/3/10.
 */
public class CustomListViewAdapter extends BaseAdapter{

    private Context context = null;

    public Context getContext() {
        return context;
    }

    public CustomListViewAdapter(Context context) {
        this.context = context;
    }

    //        private String[] data = new String[]{"1","2","3","4","5","6","7","8","9","10"};
    private CustomListCellData[] data = new CustomListCellData[]{
            new CustomListCellData("img1", "dec img1", R.drawable.img1),
            new CustomListCellData("img2", "dec img2", R.drawable.img2),
            new CustomListCellData("img3", "dec img3", R.drawable.img3),
            new CustomListCellData("img1", "dec img1", R.drawable.img1),
            new CustomListCellData("img2", "dec img2", R.drawable.img2),
            new CustomListCellData("img3", "dec img3", R.drawable.img3)
    };

    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public CustomListCellData getItem(int i) {
        return data[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

//            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>");
//
//            TextView tv = null;
//
//            if (view != null) {
//                tv = (TextView) view;
//            } else {
//                tv = new TextView(CustomListView.this);
//            }
//
//            tv.setText(getItem(i));
//            tv.setTextSize(50);
        LinearLayout ll = null;
        if (view != null) {
            ll = (LinearLayout) view;
        } else {
            ll = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.customlist_cell, null);
        }

        CustomListCellData data = getItem(i);

        ImageView icon = (ImageView) ll.findViewById(R.id.icon);
        TextView name = (TextView) ll.findViewById(R.id.name);
        TextView dec = (TextView) ll.findViewById(R.id.dec);

        icon.setImageResource(data.iconId);
        name.setText(data.name);
        dec.setText(data.dec);

        return ll;
    }
}
